# Copyright (c) 2005, 2006 Aleksandar Samardzic
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

INCLUDE(CheckFunctionExists)

SET(GETOPT getopt)
CHECK_FUNCTION_EXISTS(${GETOPT} GETOPT_FOUND)
IF(NOT GETOPT_FOUND)
  MESSAGE(FATAL_ERROR "\"${GETOPT}\" function was not found on the system.")
ENDIF(NOT GETOPT_FOUND)

OPTION(OPENMP "Use OpenMP for parallelization" ON)
IF(OPENMP)
  MESSAGE(STATUS "Check for compiler OpenMP support...")

  SET(OPENMP_FLAG)
  SET(OPENMP_FLAG_FOUND FALSE)
  SET(
    OPENMP_FLAGS
    "-fopenmp" # gcc
  )
  SET(OPENMP_FUNCTION omp_set_num_threads)
  FOREACH(FLAG ${OPENMP_FLAGS})
    IF(NOT OPENMP_FLAG_FOUND)
      MESSAGE(STATUS "Check if \"${FLAG}\" OpenMP flag working...")

      SET(CMAKE_REQUIRED_FLAGS ${FLAG})
      CHECK_FUNCTION_EXISTS(${OPENMP_FUNCTION} OPENMP_FUNCTION_FOUND${FLAG})

      IF(OPENMP_FUNCTION_FOUND${FLAG})
        SET(OPENMP_FLAG ${FLAG})
        SET(OPENMP_FLAG_FOUND TRUE)
      ENDIF(OPENMP_FUNCTION_FOUND${FLAG})
    ENDIF(NOT OPENMP_FLAG_FOUND)
  ENDFOREACH(FLAG ${OPENMP_FLAGS})

  IF(NOT OPENMP_FLAG_FOUND)
    MESSAGE(FATAL_ERROR "Given compiler does not support OpenMP.")
  ENDIF(NOT OPENMP_FLAG_FOUND)

  MESSAGE(STATUS "Check for compiler OpenMP support: yes")
  ADD_DEFINITIONS(-D USE_OPENMP)
ELSE(OPENMP)
  INCLUDE(FindThreads)
  MESSAGE(STATUS "Check for working pthreads library...")

  IF(NOT CMAKE_USE_PTHREADS_INIT)
    MESSAGE(FATAL_ERROR "pthreads library was not found on the system.")
  ENDIF(NOT CMAKE_USE_PTHREADS_INIT)

  MESSAGE(STATUS "Check for working pthreads library: found")
  ADD_DEFINITIONS(-D USE_PTHREADS)
ENDIF(OPENMP)

ADD_EXECUTABLE(
  tinos
  tinos.c
)

IF(OPENMP)
  SET_TARGET_PROPERTIES(
    tinos PROPERTIES
    COMPILE_FLAGS ${OPENMP_FLAG}
    LINK_FLAGS ${OPENMP_FLAG}
  )
ENDIF(OPENMP)

TARGET_LINK_LIBRARIES(
  tinos
  common
  input
  output
  render
  ${CMAKE_THREAD_LIBS_INIT}
  m
)

INSTALL(TARGETS tinos DESTINATION bin)
